var pagesize = 10;
var merchantid = getUrlParam('merchantid');
var areaid = getUrlParam('areaid');
$(function() {
	if(areaid == null || areaid == '') {
		alert("小区id不能为空")
		return;
	}
	if(merchantid == null || merchantid == '') {
		alert("物业id不能为空")
		return;
	}
	var data = {
		"parentid": areaid
	}
	data = JSON.stringify(data)
	var msg = ajaxpost(data, getIp() + '/public/selectBuildList')
	if("" != msg) {
		var returnString = JSON.parse(msg.returnString);
		console.log(returnString);
		createLouYu(returnString);
	}
	var data = getSelectData(1);
	var msg = ajaxpost(data, getIp() + '/doorLock/selectOpenDoorRecord')
	if("" != msg) {
		var returnString = JSON.parse(msg.returnString);
		console.log(returnString);
		createLieBiao(returnString);
		var pagecount = msg.count % pagesize;
		if(pagecount == 0) {
			pagecount = parseInt(msg.count / pagesize)
		} else {
			pagecount = parseInt(msg.count / pagesize) + 1
		}
		createPage(1, pagecount);
	}
})

function createPage(currentPage, pagecount) {
	if(0 == pagecount) {
		$('#pageLimit').html("")
	} else {
		$('#pageLimit').bootstrapPaginator({
			currentPage: currentPage, //当前的请求页面。
			totalPages: pagecount, //一共多少页。
			size: "normal", //应该是页眉的大小。
			bootstrapMajorVersion: 3, //bootstrap的版本要求。
			alignment: "right",
			numberOfPages: 10, //一页列出多少数据。
			itemTexts: function(type, page, current) { //如下的代码是将页眉显示的中文显示我们自定义的中文。
				switch(type) {
					case "first":
						return "首页";
					case "prev":
						return "上一页";
					case "next":
						return "下一页";
					case "last":
						return "末页";
					case "page":
						return page;
				}
			}
		})
	}
}
$("#pageLimit").click(function() {
	var a = $(this).children(".active").children("a").html();
	data1 = getSelectData(a);
	var msg = ajaxpost(data1, getIp() + '/doorLock/selectOpenDoorRecord')
	if("" != msg) {
		var returnString = JSON.parse(msg.returnString);
		createLieBiao(returnString);
		var pagecount = msg.count % pagesize;
		if(pagecount == 0) {
			pagecount = parseInt(msg.count / pagesize)
		} else {
			pagecount = parseInt(msg.count / pagesize) + 1
		}
		createPage(a, pagecount);
	}
})
// function createWuye(info) {
//     var len = info.length;
//     var tableinfo = '<option value="">请选择物业</option>';
//     for(var i = 0; i < info.length; i++) {
//         tableinfo += '<option value=' + info[i].organizeid + '>' + info[i].shortname + '</option>';
//     }
//     $("#select_1").html(tableinfo);
// }

function getSelectData(pagenumber) {

	var startday = $("#startday").val();
	var endday = $("#endday").val();
	var organizeid = merchantid;
	var xiaoquming = areaid;
	var louyuming = $("#select_4").val();
	var danyuanming = $("#select_5").val();
	var selectinfo = $("#selectinfo").val();
	var opentype = $("#opentype").val();
	if(opentype == "") {
		opentype = null;
	}
	var data = {
		"pagenumber": pagenumber,
		"pagesize": pagesize,
		"organizeid": organizeid,
		"startday": startday,
		"endday": endday,
		"areaid": xiaoquming,
		"buildid": louyuming,
		"units": danyuanming,
		"opentype": opentype,
		"selectinfo": selectinfo
	}
	return JSON.stringify(data)
}

function createLieBiao(info) {
	var len = info.length;
	var tableinfo = '';
	for(var i = 0; i < len; i++) {
		var opentype = '';
		if(1 == info[i].opentype) {
			opentype = "蓝牙";
		} else if(2 == info[i].opentype) {
			opentype = "云对讲";
		} else if(3 == info[i].opentype) {
			opentype = "二维码";
		} else {
			opentype = "物业app";
		}
		// tableinfo += '<td>' + info[i].organizename + '</td>';
		// tableinfo += '<td>' + info[i].areaname + '</td>';
		tableinfo += '<tr><td>' + info[i].buildname + '</td>';
		tableinfo += '<td>' + info[i].units + '</td>';
		tableinfo += '<td>' + info[i].doorlocksn + '</td>';
		tableinfo += '<td>' + info[i].doorlockname + '</td>';
		tableinfo += '<td>' + info[i].createdate + '</td>';
		tableinfo += '<td>' + opentype + '</td>';
		tableinfo += '<td>' + info[i].nickname + '</td>';
		tableinfo += '<td>' + info[i].mobileno + '</td>';
		tableinfo += '<td>' + info[i].rommcode + '</td>';
		tableinfo += '<td>' + info[i].custname + '</td></tr>';

		// tableinfo += '<td><span class="\tglyphicon glyphicon-th-list more"  id=' + info[i].custname + '></span></td>';
	}
	$("#tbody_1").html(tableinfo);

	$("#tbody_1").on("click",  ".more",  function() {
		alert($(this).parent().parent().find("td").eq(6).text());
		// var r=confirm("确定删除吗!");
		// if(r==true){
		//
		// }else{
		//     return false
		// }
		// var id = $(this).attr("id");
		// var data = {
		//     doorlockid: id
		// }
		// data = JSON.stringify(data);
		// var msg = ajaxpost(data, getIp() + '/doorLock/deleteDoorLock');
		// if("" != msg) {
		//     alert("删除成功")
		//     var data1 = getSelectData(1);
		//     var msg = ajaxpost(data1, getIp() + '/doorLock/selectDoorLockList')
		//     alert(msg)
		//     if("" != msg) {
		//         var returnString = JSON.parse(msg.returnString);
		//         createLieBiao(returnString);
		//         var pagecount = msg.count % pagesize;
		//         if(pagecount == 0) {
		//             pagecount = parseInt(msg.count / pagesize)
		//         } else {
		//             pagecount = parseInt(msg.count / pagesize) + 1
		//         }
		//         createPage(1, pagecount);
		//     }
		// }
	});
}
//初始化开始日期控件
var start = laydate.render({
	elem: '#startday',
	max: new Date().toLocaleDateString(),
	done: function(value, date, endDate) {
		end.config.min = {
			year: date.year,
			month: date.month - 1,
			date: date.date
		}; //重置结束日期最小值
	}
});
//初始化结束日期控件
var end = laydate.render({
	elem: '#endday',
	//      max: new Date().toLocaleDateString(),
	done: function(value, date, endDate) {
		//结束时期清空后，重置开始日期最大值
		if(null == $('#endday').val() || '' == $('#endday').val()) {
			var today = new Date();
			start.config.max = {
				year: today.getFullYear(),
				month: today.getMonth(),
				date: today.getDate()
			}; //重置开始日期最大值
		} else {
			start.config.max = {
				year: date.year,
				month: date.month - 1,
				date: date.date
			}; //重置开始日期最大值
		}

	}
});
//
// $("#select_1").change(function() {
//     if(this.value==""){
//         return;
//     }
//     var data = {
//         "parentid": this.value
//     }
//     data = JSON.stringify(data)
//     var msg = ajaxpost(data, getIp() + '/public/selectPropertyCommunityList')
//     if("" != msg) {
//         var returnString = JSON.parse(msg.returnString);
//         console.log(returnString);
//         createXiaoQu(returnString);
//         $("#select_4").html('<option value="">请选择楼宇</option>');
//         $("#select_5").html('<option value="">请选择单元</option>');
//     }
//
// });
// $("#select_2").change(function() {
//     if(this.value==""){
//         return;
//     }
//     var data = {
//         "parentid": this.value
//     }
//     data = JSON.stringify(data)
//     var msg = ajaxpost(data, getIp() + '/public/selectBuildList')
//     if("" != msg) {
//         var returnString = JSON.parse(msg.returnString);
//         console.log(returnString);
//         createLouYu(returnString);
//         $("#select_5").html('<option value="">请选择单元</option>');
//     }
//
// });
$("#select_4").change(function() {
	if(this.value != "") {
		var data = {
			"parentid": this.value
		}
		data = JSON.stringify(data)
		var msg = ajaxpost(data, getIp() + '/public/selectUnitsList')
		if("" != msg) {
			var returnString = JSON.parse(msg.returnString);
			createDanYuan(returnString);
		}
	}

});

function createXiaoQu(info) {
	var len = info.length;
	var tableinfo = '<option value="">请选择小区</option>';
	for(var i = 0; i < info.length; i++) {
		tableinfo += '<option value=' + info[i].areaid + '>' + info[i].areaname + '</option>';
	}
	$("#select_2").html(tableinfo);
}

function createLouYu(info) {
	var len = info.length;
	var tableinfo = '<option value="">请选择楼宇</option>';
	for(var i = 0; i < info.length; i++) {
		tableinfo += '<option value=' + info[i].id + '>' + info[i].buildname + '</option>';
	}
	$("#select_4").html(tableinfo);
}

function createDanYuan(info) {
	var tableinfo = '<option value="">请选择单元</option>';
	var len = info.length;
	for(var i = 0; i < len; i++) {
		tableinfo += '<option value=' + info[i].id + '>' + info[i].unitname + '</option>';
	}
	$("#select_5").html(tableinfo);
}

$("#select").click(function() {
	data1 = getSelectData(1);
	var msg = ajaxpost(data1, getIp() + '/doorLock/selectOpenDoorRecord')
	if("" != msg) {
		var returnString = JSON.parse(msg.returnString);
		console.log(returnString);
		createLieBiao(returnString);
		var pagecount = msg.count % pagesize;
		if(pagecount == 0) {
			pagecount = parseInt(msg.count / pagesize)
		} else {
			pagecount = parseInt(msg.count / pagesize) + 1
		}

		createPage(1, pagecount);
	}
})